package com.example.blog.modules.user.controller;

import com.example.blog.common.api.ApiResponse;
import com.example.blog.modules.user.dto.LoginRequest;
import com.example.blog.modules.user.dto.RegisterRequest;
import com.example.blog.modules.user.dto.ResetPasswordRequest;
import com.example.blog.modules.user.dto.UserDTO;
import com.example.blog.modules.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "认证接口", description = "包含注册、登录等认证相关接口")
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {
    
    private final UserService userService;
    
    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public ApiResponse<UserDTO> register(@Valid @RequestBody RegisterRequest request) {
        return ApiResponse.success(userService.register(request));
    }
    
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public ApiResponse<String> login(@Valid @RequestBody LoginRequest request) {
        return ApiResponse.success(userService.login(request));
    }
    
    @Operation(summary = "邮箱验证")
    @GetMapping("/verify-email")
    public ApiResponse<Boolean> verifyEmail(@RequestParam String token) {
        return ApiResponse.success(userService.verifyEmail(token));
    }
    
    @Operation(summary = "发送验证邮件")
    @PostMapping("/send-verification-email")
    public ApiResponse<Void> sendVerificationEmail(@RequestParam String email) {
        userService.sendVerificationEmail(email);
        return ApiResponse.success();
    }
    
    @Operation(summary = "请求重置密码")
    @PostMapping("/request-password-reset")
    public ApiResponse<Void> requestPasswordReset(@RequestParam String email) {
        userService.resetPassword(email);
        return ApiResponse.success();
    }
    
    @Operation(summary = "重置密码")
    @PostMapping("/reset-password")
    public ApiResponse<Void> resetPassword(@Valid @RequestBody ResetPasswordRequest request) {
        userService.updatePassword(request.getToken(), request.getNewPassword());
        return ApiResponse.success();
    }
} 